<template>
  <div>
    <header id="head">
      <div class="title">朔光影评网</div>
      <div class="select">
        <div
          v-for="(item, index) in arr"
          :key="index"
          :class="item.color"
          @click="Onclick(index)"
        >
          {{ item.name }}
        </div>
      </div>
      <div>
        <el-input
          placeholder="请输入内容"
          size="small"
          class="input"
          v-model="input"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            class="btn"
            size="small"
            @click="que"
          ></el-button>
        </el-input>
      </div>
      <div class="gin" v-show="login">
        <span @click="dialogFormVisible = true">登录</span> |
        <span @click="dialogVisible = true">注册</span>
      </div>
      <div class="gin dss" v-show="islog" @click="jump()">
        欢迎你 {{ user }}
        <ul class="yys">
          <li @click.stop="tuichu">退出登录</li>
          <li @click.stop="dialog = true">修改密码</li>
        </ul>
      </div>
    </header>
    <!-- 登录 -->
    <el-dialog title="登录" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth" class="pwd">
          <el-input v-model="form.userpwd" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="logins">登 录</el-button>
      </div>
    </el-dialog>

    <!-- 注册 -->
    <el-dialog title="注册" :visible.sync="dialogVisible" width="30%">
      <el-form :model="forms">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="forms.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth" class="pwd">
          <el-input v-model="forms.userpwd" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" :label-width="formLabelWidth" class="pwd">
          <el-input v-model="forms.phone" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="(dialogVisible = false), crea()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 修改密码弹框 -->
    <el-dialog title="密码修改" :visible.sync="dialog" width="30%">
      <el-form :model="forms">
        <el-form-item label="新密码" :label-width="formLabelWidth">
          <el-input v-model="aa" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="确认密码"
          :label-width="formLabelWidth"
          class="pwd"
        >
          <el-input v-model="pwd.userpwd" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog = false">取 消</el-button>
        <el-button type="primary" @click="(dialog = false), uppwd()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { log, create, uppwd } from "@/api/index";
export default {
  data() {
    return {
      arr: [
        {
          name: "首页",
          color: "on",
        },
        {
          name: "分类",
          color: "",
        },
        {
          name: "榜单",
          color: "",
        },
      ],
      input: "",
      login: true,
      islog: false,
      dialogFormVisible: false,
      dialogVisible: false,
      userid: 0,
      user: "",
      form: {
        username: "",
        userpwd: "",
      },
      forms: {
        username: "",
        userpwd: "",
        phone: "",
      },
      formLabelWidth: "120px",
      xians: false,
      dialog: false,
      aa: "",
      pwd: {
        userpwd: "",
      },
    };
  },
  created() {
    if (sessionStorage.getItem("userid")) {
      this.islog = true;
      this.login = false;
      this.user = sessionStorage.getItem("user");
    }
  },
  methods: {
    Onclick(index) {
      this.arr[0].color = "";
      this.arr[1].color = "";
      this.arr[2].color = "";
      if (index !== "") {
        this.arr[index].color = "on";
      }
      if (index === 0) {
        this.$router.push("/");
      }
      if (index === 2) {
        this.$router.push("/Rankinglist");
      }
      if (index === 1) {
        this.$router.push("/Classs");
      }
    },
    //登录
    logins() {
      log(this.form).then((res) => {
        if (res.code == 0) {
          this.login = false;
          this.islog = true;
          this.userid = res.data;
          this.user = res.data1;
          sessionStorage.setItem("userid", this.userid[0].id);
          sessionStorage.setItem("user", res.data1);
          this.open3();
        }
        this.dialogFormVisible = false;
      });
    },
    //退出登录
    tuichu() {
      this.islog = false;
      this.login = true;
      sessionStorage.removeItem("user");
      sessionStorage.removeItem("userid");
      this.$router.push("/");
    },
    jump() {
      this.$router.push("/usermain");
    },
    //注册
    crea() {
      create(this.forms).then((res) => {
        if (res.code == 0) {
          this.open2();
        }
      });
    },
    //修改密码
    uppwd() {
      uppwd(sessionStorage.getItem("userid"), this.pwd).then((res) => {
        if (res.code == 0) {
          this.open4();
        }
      });
    },
    //搜索
    que() {
      if (this.$route.path !== "/Classs") {
        this.$router.push({
          path: "/Classs",
          query: {
            keyname: this.input,
          },
        });
      } else {
        this.$router.push({
          path: "/Class",
          query: {
            keyname: this.input,
          },
        });
      }
    },
    open2() {
      this.$message({
        message: "恭喜你，注册成功",
        type: "success",
      });
    },
    open3() {
      this.$message({
        message: "登录成功",
        type: "success",
      });
    },
    open4() {
      this.$message({
        message: "修改成功",
        type: "success",
      });
    },
  },
};
</script>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
}
.on {
  color: palegreen;
}
#head {
  height: 60px;
  background: #1e2736;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  .title {
    color: #fff;
    font-size: 20px;
    flex: 1;
    text-align: center;
  }
  .select {
    color: #fff;
    display: flex;
    flex: 2;
    div {
      margin-left: 25px;
      cursor: pointer;
      &:hover {
        color: palegreen;
      }
    }
  }
  .input {
    width: 300px;
    flex: 3;
    .btn {
      width: 60px;
    }
  }
  .gin {
    flex: 1;
    color: #fff;
    text-align: center;
    cursor: pointer;
    position: relative;
    span:hover {
      color: palegreen;
    }
  }
  .dss {
    &:hover .yys {
      display: block;
    }
  }
}
.pwd {
  margin-top: 20px;
}
.yys {
  position: absolute;
  width: 100px;
  color: #fff;
  list-style: none;
  left: 35%;
  display: none;
  z-index: 2;
  li {
    height: 50px;
    line-height: 50px;
    text-align: center;
    background: #1e2736;
    &:hover {
      color: palegreen;
    }
  }
}
</style>>

